package com.ryskoo.customer.vendorbasic.service;

import com.ryskoo.customer.vendorbasic.domain.VendorBasicPO;
import com.ryskoo.customer.vendorbasic.repository.IVendorBasicRepository;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.security.InvalidParameterException;


@Service
public class VendorBasicServiceImpl extends BasicServiceImpl<VendorBasicPO> implements IVendorBasicService {

    @Autowired
    private IVendorBasicRepository vendorBasicRepository;

    @PostConstruct
    public void init(){
        this.setDao(vendorBasicRepository);
    }

    
	/**
	 * 根据 VendorUuid 查询 vendorBasic
	 * 
	 * @param VendorUuid
	 * @return
	 */
	@Override
	public VendorBasicPO getByVendorUuid(String vendorUuid) {
		return vendorBasicRepository.getByVendorUuid(vendorUuid);
	}


	
	/**
	 * 商户外卖设置
	 */
	@Override
	public VendorBasicPO updataVendorBasic(VendorBasicPO po) {
		if (null != po && !StringUtils.isEmpty(po.getVendorId())) {
			VendorBasicPO oldPo = vendorBasicRepository.getByVendorUuid(po.getVendorId());
			if (null == oldPo) {
				throw new InvalidParameterException("商户不存在");
			}
			oldPo.setSupportOut(po.isSupportOut());
			oldPo.setSupportScope(po.getSupportScope());
			oldPo.setDistributionPrice(po.getDistributionPrice());
			oldPo.setLeastPrice(po.getLeastPrice());
			oldPo.setReachPrice(po.getReachPrice());
			oldPo.setBeginTime(po.getBeginTime());
			oldPo.setEndTime(po.getEndTime());
			vendorBasicRepository.merge(oldPo);
			
			return oldPo;
		} else {
			throw new InvalidParameterException("商户不存在");
		}
	}

}
